//
//  UINavigationBar+Background.m
//  TexturedNavBar
//
//  Created by Francois Proulx on 10-06-14.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "UINavigationBar+Background.h"

#import <objc/runtime.h>
#import "NSObject+SupersequentImplementation.h"

@implementation UINavigationBar (Background)

static char backgroundImageKey;

- (void) setBackgroundImage:(UIImage *)anImage
{
	// Remove any previously set background and add an associative reference
	objc_setAssociatedObject(self, &backgroundImageKey, nil, OBJC_ASSOCIATION_ASSIGN);
	objc_setAssociatedObject(self, &backgroundImageKey, anImage, OBJC_ASSOCIATION_RETAIN);
}

- (UIImage *) backgroundImage
{
	UIImage *backgroundImage = (UIImage *) objc_getAssociatedObject(self, &backgroundImageKey);
	return backgroundImage;
}
- (void) drawRect:(CGRect)rect 
{
    UIImage *backgroundImage = [self backgroundImage];
	if (backgroundImage) {
		CGRect rect = self.bounds;
		[backgroundImage drawInRect:CGRectMake(0, 0, rect.size.width, rect.size.height)];
	} else {
		// Call [super drawRect:rect];
		IMP superSequentImp = [self getImplementationOf:_cmd after:impOfCallingMethod(self, _cmd)];
		((void(*)(id, SEL, CGRect))superSequentImp)(self, _cmd, rect);
	}
} 

@end
